﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Web.Controls;
using BySoft.Server.Consts;
using OneSoft.Web.UI.WebControls;
using BySoft.Server.Common;
using BySoft.Server.Business.UserCode;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMMaterialInStock : BasePage
    {
        #region 变量定义
        int m_count = 0;
     
        #endregion

        #region 属性定义
        private DataTable DtMatInStockData
        {
            get
            {
                if (null == Session["DtMatInStockData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtMatInStockData"];
                }
            }
            set
            {
                Session["DtMatInStockData"] = value;
            }
        }

        private DataTable DtMatInStockDataTemp
        {
            get
            {
                if (null == Session["DtMatInStockDataTemp"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtMatInStockDataTemp"];
                }
            }
            set
            {
                Session["DtMatInStockDataTemp"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("PurchaseDetailID");//1 
            dt.Columns.Add("PurchaseID");//2 
            dt.Columns.Add("WorkOrderNo");//4
            dt.Columns.Add("MaterialID");//5
            dt.Columns.Add("MaterialName");//6
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("PurchaseNum");//9
            dt.Columns.Add("LeftNums");//10
            dt.Columns.Add("Nums");//11
            dt.Columns.Add("Price");//12
            dt.Columns.Add("Amount");//13
            dt.Columns.Add("MMNumber");//13
            dt.Columns.Add("SingleMMNumber");//13
            dt.Columns.Add("IsNew");//14 
            dt.Columns.Add("IsDel");//14
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        { 
            string strFrom = CommonMethod.GetStartTime(cpFrom.Value);
            string strTo = CommonMethod.GetEndTime(cpTo.Value);
            DataTable dtData = BsMatInStockBusiness.SelectMat(txtPurchaseID.Text.Trim(), strFrom, strTo, txtWorkOrderNo.Text.Trim(), txtMaterialName.Text.Trim(), hidSupplierID.Value, hidMatInstockID.Value, pageIndex, pcData.PageSize, out m_count);

            pcData.RecordCount = m_count;
            if (dtData.Rows.Count > 0)
            {
                grdResult.DataSource = dtData;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
                pcData.Visible = true;
                EnableGridViewRow(grdResult, DtMatInStockDataTemp, "PurchaseDetailID", false); 
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dtData);
                pcData.Visible = false;
            }
        }

        public void EnableGridViewRow(OneSoft.Web.UI.WebControls.SGridView sgv, DataTable dt, string strCompareField, bool bEnable)
        {
            if (null == dt || dt.Rows.Count < 1)
            {
                return;
            }
            DataTable dtTemp = dt.Copy();
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (CommonMethod.GetString(dr["IsDel"])=="1")
                {
                    continue;
                }
                foreach (GridViewRow gr in sgv.Rows)
                {
                    Label lblCompareField = gr.FindControl("lbl" + strCompareField) as Label;
                    if (lblCompareField.Text.Trim().Equals(dr[strCompareField].ToString().Trim()))
                    {
                        if (gr.FindControl("cbChoose") != null)
                            ((CheckBox)gr.FindControl("cbChoose")).Checked = true;
                        if (bEnable)
                            gr.Enabled = false;
                        if (dr["CheckStatus"].ToString().Trim()!="2")
                            gr.Enabled = false;
                        break;
                    }
                }
            }
        }

        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidMatInstockID.Value = UIHelper.GetQueryStringValue("mid"); 
                DtMatInStockDataTemp = null;
                DtMatInStockDataTemp = DtMatInStockData.Copy();
                hidSupplierID.Value = UIHelper.GetQueryStringValue("id"); 
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                BindData(0);
            }
        } 

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            if (DtMatInStockDataTemp.Rows.Count < 1)
            {
                UIHelper.Alert(this.Page, String.Format(Message.PleaseSelect, "原料信息"));
                return;
            } 
            DtMatInStockData = DtMatInStockDataTemp.Copy();  
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success',1);CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }
        #endregion

        #region 本地方法
        private void AddPro()
        {
            DtMatInStockDataTemp = GetAddPro(DtMatInStockDataTemp, "PurchaseDetailID");
        }

        private DataTable GetAddPro(DataTable dt, string strCompareField)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblCompareField = gvr.FindControl("lbl" + strCompareField) as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        if ((dtTemp.Select(string.Format("{0}='{1}' and IsDel<>'1'", strCompareField, lblCompareField.Text))).Length == 0)
                        {

                            Label lblPurchaseID = gvr.FindControl("lblPurchaseID") as Label;
                            Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                            Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                            Label lblMaterialName = gvr.FindControl("lblMaterialName") as Label;
                            Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                            Label lblUnit = gvr.FindControl("lblUnit") as Label;
                            Label lblSingleMMNumber = gvr.FindControl("lblSingleMMNumber") as Label;
                            Label lblPurchaseNum = gvr.FindControl("lblPurchaseNum") as Label;
                            Label lblLeftNums = gvr.FindControl("lblLeftNums") as Label;
                            Label lblPrice = gvr.FindControl("lblPrice") as Label;

                            DataRow dr = dtTemp.NewRow();
                            dr["BillDetailID"] = "-1";
                            dr["PurchaseDetailID"] = GetGridViewString(lblCompareField);
                            dr["PurchaseID"] = GetGridViewString(lblPurchaseID);
                            dr["WorkOrderNo"] = GetGridViewString(lblWorkOrderNo);
                            dr["MaterialID"] = GetGridViewString(lblMaterialID);
                            dr["MaterialName"] = GetGridViewString(lblMaterialName);
                            dr["Specifications"] = GetGridViewString(lblSpecifications);
                            dr["Unit"] = GetGridViewString(lblUnit);
                            dr["SingleMMNumber"] = GetGridViewString(lblSingleMMNumber);
                            dr["PurchaseNum"] = GetGridViewString(lblPurchaseNum);
                            dr["LeftNums"] = GetGridViewString(lblLeftNums);
                            dr["Price"] = GetGridViewString(lblPrice);
                            decimal dLeftNums = CommonMethod.ConvertToDecimal(GetGridViewString(lblLeftNums));
                            decimal dPrice = CommonMethod.ConvertToDecimal(GetGridViewString(lblPrice));
                            decimal dRevNum = 0;
                            dRevNum = dLeftNums;
                            decimal dAmount = Math.Round(dRevNum * dPrice, 2);
                            decimal SingleMMNumber = CommonMethod.ConvertToDecimal(GetGridViewString(lblSingleMMNumber));
                            decimal dMMNumber = Math.Round(SingleMMNumber * dRevNum, 2);
                            dr["Nums"] = dRevNum;
                            dr["Amount"] = dAmount;
                            dr["MMNumber"] = dMMNumber;
                            dr["IsNew"] = "1";
                            dr["IsDel"] = "-1";
                            dtTemp.Rows.Add(dr);
                        } 
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select(string.Format("{0}='{1}' and IsDel<>'1'", strCompareField, lblCompareField.Text));
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        #endregion
    }
}

